<?php
	
	function dbconnect() {
		$dbconnect=  	mysql_connect("localhost","root","")
			or die("Could not connect: ".mysql_error());
	
		mysql_select_db("db_mk",$dbconnect)
			or die("Could not find database: ".mysql_errno());
	
		mb_internal_encoding("UTF-8");
		mysql_query("SET 	character_set_results = 'utf8',
						 	character_set_client = 'utf8', 
							character_set_connection = 'utf8', 
							character_set_database = 'utf8', 
							character_set_server = 'utf8';", $dbconnect);
		
		return $dbconnect;
	}
	
	
	function addArticle ($art_name,$art) {
		$db = dbconnect();
		mysql_query("insert into 	articles	(articles_name,		articles_text)
									values		('$art_name',	'$art'	)",$db)
				or die ("error".mysql_error());
	
	}
	
	
	function findName($table){
		$db = dbconnect();
		$quary = mysql_query("	select ".$table."_name
								from $table
								order by ".$table."_name",$db);
		$i=0;
		while ($arrdb = mysql_fetch_row($quary)){
			$arr[$i]=$arrdb[0];
			$i++;
		}
		return $arr;
	}
	
	
	function addNews ($n_id,$news_name,$news_order,$news_text,$news_vis,$news_image_name) {
		$db = dbconnect();
		mysql_query("	insert into 	news  (n_id,	news_name,		news_order,		news_text,		news_vis,	news_image_name		)
										values($n_id,	'$news_name',	$news_order,	'$news_text',	$news_vis,	'$news_image_name'	)",$db)
				or die ("error".mysql_error());
	}
	

	
	function takeId($name,$table){
		$db=dbconnect();
		$n = substr($table, 0 ,1);
		$quary = mysql_query("	select ".$n."_id
								from $table
								where ".$table."_name = '$name'",$db)
				or die ("error".mysql_error());
		return mysql_result($quary, 0, $n."_id");
	}
	
	
	
	function checkEmptyFields(){
		
		if (!isset($_POST['newsvisible'])) {
			$_POST['newsvisible']=0;
		} else {
			$_POST['newsvisible']=1;
		}
		if ($_POST['order']=="") {
			$_POST['order']=null;
		}
	}

	
	
	
	function takeRecord($numb,$column,$table){		
		$db = dbconnect();
		$n = substr($table, 0 ,1);
		$quary = mysql_query("	select  ".$table."_$column
								from $table
								where ".$n."_id = $numb	",$db) or die ("error".mysql_error());
		$text = mysql_result($quary, 0, $table."_".$column);
		return $text;
		
	}
	
	
	function updateArticles($id,$name,$text){
		$db = dbconnect();
		$quary = mysql_query("	update articles 
								set articles_text = '$text',
									articles_name = '$name'
								where a_id = $id  ",$db) or die ("error".mysql_error());
	}
	
	function updateNews($id,$name,$order,$text,$vis,$image_name) {
		$db = dbconnect();
		$quary = mysql_query("	update news
								set news_text = '$text',
									news_name = '$name'		
									news_order = $order,			
									news_vis = $vis,	
									news_image_name = '$image_name'	
									where n_id = $id  ",$db) or die ("error".mysql_error());
	}
	
	function deleteRecord($id,$table){
		$db = dbconnect();
		$n = substr($table, 0 ,1);
		$quary = mysql_query("delete from $table where ".$n."_id = $id",$db) or die ("error".mysql_error());
	}
	
?>